﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;
using Button = UnityEngine.UI.Button;
using Image = UnityEngine.UI.Image;

public class IconCard : BasicCard
{
    public GameObject _belongSlot;

    private Image _icon;
    private Button _button;

    private bool _isListening = false;
    private bool _isDisplayingOverlay = false;
    
    public void SetBelongSlot(GameObject belongSlot)
    {
        _belongSlot = belongSlot;
    }

    private void Update()
    {
        if (!_isListening)
        {
            if (_isDisplayingOverlay)
            {
                HideTargetGrid();
                TileManager.HideAllTileOverlay();
            }
            
            return;
        }
        
        SelectTarget();

        if (Input.GetMouseButtonUp(1))
        {
            ConfirmTarget();
            _isListening = false;
        }
    }

    public void BindCardToButton()
    {
        _button = _belongSlot.GetComponent<Button>();
        _icon = _belongSlot.transform.GetChild(0).GetComponent<Image>();
        
        _icon.sprite = pairData.cardData.icon;
        
        _button.onClick.AddListener(delegate { _isListening = !_isListening; });
    }


    public void SelectTarget()
    {
        ShowTargetGridAtMousePosition();
        _isDisplayingOverlay = true;

        caller.isHoldingCard = true;
        TileManager.ShowAttackRange(GameObject.FindGameObjectWithTag(StaticDataManager.PLAYER_TAG), pairData.cardData.range, pairData.cardData.rangeType, StaticDataManager.TILE_TAG);
    }

    public void ConfirmTarget()
    {
        HideTargetGrid();
        TileManager.HideAllTileOverlay();
        _isDisplayingOverlay = false;
        
        caller.isHoldingCard = false;

        ExecuteCard();
        
        TileManager.ShowSelectableTiles();
    }


}
